package model;

public class Item {
	private int itemID;
	private String itemName;
	private int itemPrice;
	private int itemStock;
	private String itemURL;
	private String itemDescription;
	
	public Item(int id, String name, String url, int price, int stock, String description) {
		itemID = id;
		itemName = name;
		itemURL = url;
		itemPrice = price;
		itemStock = stock;
		itemDescription = description;
	}

	public int getItemID() {
		return itemID;
	}

	public String getItemName() {
		return itemName;
	}

	public void setItemName(String itemName) {
		this.itemName = itemName;
	}

	public int getItemPrice() {
		return itemPrice;
	}

	public void setItemPrice(int itemPrice) {
		this.itemPrice = itemPrice;
	}

	public int getItemStock() {
		return itemStock;
	}

	public void setItemStock(int itemStock) {
		this.itemStock = itemStock;
	}

	public String getItemURL() {
		if (itemURL == null || itemURL.equals(""))
			return "http://webbies.dk/apocalypse/images/duck.jpg";
		return itemURL;
	}
	/**
	 * Converts the item url to the path to the featured image (the correct size). F.eks. changing http://example.com/test.jpg to http://example.com/test-featured.jpg 
	 * @return Image URL to the featured image. 
	 */
	public String getFeaturedImageURL()
	{
		try {
			String[] split = getItemURL().split("\\.");
			String res = "";
			for (int i = 0; i < split.length - 1; i++)
			{
				res += split[i] + ".";
			}
			res = res.substring(0, res.length() - 1);
			res += "-featured";
			res += "." + split[split.length -1];
			return res;
		}
		catch (StringIndexOutOfBoundsException e)
		{
			return "";
		}
	}
	/**
	 * Converts the item url to the path to the thumb image (the correct size). F.eks. changing http://example.com/test.jpg to http://example.com/test-thumb.jpg 
	 * @return Image URL to the thumb image.
	 */
	public String getThumbImageURL()
	{
		try {
			String[] split = getItemURL().split("\\.");
			String res = "";
			for (int i = 0; i < split.length - 1; i++)
			{
				res += split[i] + ".";
			}
			res = res.substring(0, res.length() - 1);
			res += "-thumb";
			res += "." + split[split.length -1];
			return res;
		}
		catch (StringIndexOutOfBoundsException e)
		{
			return "";
		}
	}

	public void setItemURL(String itemURL) {
		this.itemURL = itemURL;
	}
	
	public void setItemID(int id) {
		this.itemID = id;
	}

	public String getItemDescription() {
		return itemDescription;
	}

	public void setItemDescription(String itemDescription) {
		this.itemDescription = itemDescription;
	}
}
